/**
 * Created by liuqingxia on 2016/5/30.
 */

$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
        var value = parseInt(row['executeType']);
        if (ExecuteType.isCron(value)) {
            return cronBtn;
        }
        if (ExecuteType.isOnce(value)) {
            return onceBtn;
        }
    }

    LTS.colFormatter.stateFormat = function (v, row) {
        var state = row['state'];
        if (typeof(state) == "undefined") {
            return "状态未知";
        }
        state = $.trim(state);
        if (state === "") {
            return "状态未知";
        }
        state = parseInt(state);
        if (state < 0 || state > 6) {
            if (state == 255) {
                return "UNKNOWN(255)";
            } else {
                return "状态未知";
            }
        }
        var result = switchStateToCN(state);
        return result;
    }

    LTS.colFormatter.executeType = function (v, row) {
        return ExecuteType.getTypeStr(parseInt(row['executeType']));
    }

    var ltsTable = $("#job-table-container").ltsTable({
        url: url,
        templateId: 'job-table'
    });

    /*点击查询按钮*/
    $(document).on("click", "#searchBtn", function () {
        var params = getFormParams("job-find-form");
        ltsTable.post(params, 1);
    });

    /*用户一访问页面，就搜索显示列表页*/
    $("#searchBtn").trigger("click");

    /*点击提交按钮*/
    $(document).on("click", ".start-btn", function () {
        operationByUrl("/job/start", $(this),ltsTable);
    });

    /*点击立即执行按钮*/
    $(document).on("click", ".start-now-btn", function () {
        operationByUrl("/job/trigger", $(this),ltsTable);
    });

    /*点击暂停按钮*/
    $(document).on("click", ".pause-btn", function () {
        operationByUrl("/job/pause", $(this),ltsTable);
    });

    /*点击回复按钮*/
    $(document).on("click", ".resume-btn", function () {
        operationByUrl("/job/resume", $(this),ltsTable);
    });

    /*点击删除按钮*/
    $(document).on("click", ".remove-btn", function () {
        removeJobId = getCurrentId($(this));
        $("#remove-tip-modal").modal("show");
    });

    /*点击确认删除按钮*/
    $(document).on("click", "#confirm-remove-btn", function () {
        $("#cancel-remove-btn").trigger("click");//提示框隐藏
        var params = {};
        params['id'] = $.trim(removeJobId);
        $.postHttp(params, getUrlBySuffix("/job/remove"), function(json){
        	if (json && json.success) {
        		swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				var param = getFormParams("job-find-form");
                	ltsTable.post(param, pageNum); 
                	$("#remove-tip-modal").modal("hide");
    			});
            } else {
                if (json) {
                    swal("失败", json['message'], 'error');
                    $("#remove-tip-modal").modal("hide");
                }
            }
        });
        //ltsTable.post({}, 1);//重新显示列表页
    });

    /*点击编辑按钮*/
    $(document).on("click", ".update-btn", function () {
        var id = getCurrentId($(this));
        var job = getJobById(id);
        fillJobEditForm(job);//将Job对象各属性值赋值到任务编辑表单
        $("#job-edit-modal").modal("show");//显示任务编辑模态框
    });

    /*点击任务编辑模态框上的修改按钮*/
    $(document).on("click", "#edit-btn-modal", function () {
        var params = getJobFormParams("editForm");
        $.postHttp(params, getUrlBySuffix("/job/update"), function(json){
        	if (json && json.success) {
        		swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				var param = getFormParams("job-find-form");
                	ltsTable.post(param, pageNum); 
                	$("#job-edit-modal").modal("hide");
                	
    			});
                
                
        	} else {
                if (json) {
                    swal("失败", json['message'], 'error');
                    $("#job-edit-modal").modal("show");
                }
            }
        });
        
        //$("#searchBtn").trigger("click");
    });

    /*点击终止按钮*/
    $(document).on("click", ".interrupt-btn", function () {
        operationByUrl("/job/interrupt", $(this),ltsTable);
    });

    /*点击明细按钮*/
    $(document).on("click", ".history-btn", function () {
        var jobId = getCurrentId($(this));
        var url = getUrlBySuffix("/job/history/find");
        window.location.href = url + "?jobId=" + jobId;//跳转到明细显示页面
    });

    selectExecuteTypeRadio();
    clickJobEditCancelBtn();

});